package com.xmall.service.impl;

import com.xmall.bean.table.System;
import com.xmall.bean.table.SystemExample;
import com.xmall.mapper.SystemMapper;
import com.xmall.service.ConfigService;
import com.xmall.service.util.ConfigUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Connor
 * @ClassName: ConfigServiceImpl
 * @PackageName: com.xmall.service.admin
 * @date 2020/12/06
 * @Version 1.0
 */
@Service
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    SystemMapper systemMapper;

    @Override
    public Map<String, String> queryMallConfiguration() {
        List<System> systemList = ConfigUtils.invoke(systemMapper,"cskaoyan_mall_mall_");
        HashMap<String, String> map = new HashMap<>(16);
        for (System system : systemList) {
            if ("cskaoyan_mall_mall_name".equals(system.getKeyName())) {
                map.put("cskaoyanmall_mall_name", system.getKeyValue());
            }
            if ("cskaoyan_mall_mall_qq".equals(system.getKeyName())) {
                map.put("cskaoyanmall_mall_qq", system.getKeyValue());
            }
            if ("cskaoyan_mall_mall_phone".equals(system.getKeyName())) {
                map.put("cskaoyanmall_mall_phone", system.getKeyValue());
            }
            if ("cskaoyan_mall_mall_address".equals(system.getKeyName())) {
                map.put("cskaoyanmall_mall_address", system.getKeyValue());
            }
        }
        return map;
    }

    @Override
    public void updateMallConfiguration(Map<String, String> map) {
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        System system = new System();
        for (Map.Entry<String, String> entry : entrySet) {
            SystemExample systemExample = new SystemExample();
            SystemExample.Criteria criteria = systemExample.createCriteria();
            if (entry.getKey().endsWith("mall_name")) {
                criteria.andKeyNameEqualTo("cskaoyan_mall_mall_name");
            }
            if (entry.getKey().endsWith("mall_qq")) {
                criteria.andKeyNameEqualTo("cskaoyan_mall_mall_qq");
            }
            if (entry.getKey().endsWith("mall_phone")) {
                criteria.andKeyNameEqualTo("cskaoyan_mall_mall_phone");
            }
            if (entry.getKey().endsWith("mall_address")) {
                criteria.andKeyNameEqualTo("cskaoyan_mall_mall_address");
            }
            system.setKeyValue(entry.getValue());
            system.setUpdateTime(new Date());
            systemMapper.updateByExampleSelective(system, systemExample);
        }

    }

    @Override
    public Map<String, String> queryExpressConfiguration() {
        List<System> systemList = ConfigUtils.invoke(systemMapper,"cskaoyan_mall_express_");
        Map<String, String> map = new HashMap<>(16);
        for (System system : systemList) {
            if ("cskaoyan_mall_express_freight_min".equals(system.getKeyName())) {
                map.put("cskaoyanmall_express_freight_min", system.getKeyValue());
            }
            if ("cskaoyan_mall_express_freight_value".equals(system.getKeyName())) {
                map.put("cskaoyanmall_express_freight_value", system.getKeyValue());
            }
        }
        return map;
    }

    @Override
    public void updateExpressConfiguration(Map<String, String> map) {
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        System system = new System();
        for (Map.Entry<String, String> entry : entrySet) {
            SystemExample systemExample = new SystemExample();
            SystemExample.Criteria criteria = systemExample.createCriteria();
            if (entry.getKey().endsWith("express_freight_value")) {
                criteria.andKeyNameEqualTo("cskaoyan_mall_express_freight_value");
            }
            if (entry.getKey().endsWith("express_freight_min")) {
                criteria.andKeyNameEqualTo("cskaoyan_mall_express_freight_min");
            }
            system.setKeyValue(entry.getValue());
            system.setUpdateTime(new Date());
            systemMapper.updateByExampleSelective(system, systemExample);
        }
    }

    @Override
    public Map<String, String> queryOrderConfiguration() {
        List<System> systemList = ConfigUtils.invoke(systemMapper,"cskaoyan_mall_order_");
        Map<String, String> map = new HashMap<>(16);
        for (System system : systemList) {
            if ("cskaoyan_mall_order_unconfirm".equals(system.getKeyName())) {
                map.put("cskaoyanmall_order_unconfirm", system.getKeyValue());
            }
            if ("cskaoyan_mall_order_unpaid".equals(system.getKeyName())) {
                map.put("cskaoyanmall_order_unpaid", system.getKeyValue());
            }
            if ("cskaoyan_mall_order_comment".equals(system.getKeyName())) {
                map.put("cskaoyanmall_order_comment", system.getKeyValue());
            }
        }
        return map;
    }

    @Override
    public void updateOrderConfiguration(Map<String, String> map) {
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        System system = new System();
        for (Map.Entry<String, String> entry : entrySet) {
            SystemExample systemExample = new SystemExample();
            SystemExample.Criteria criteria = systemExample.createCriteria();
            if (entry.getKey().endsWith("order_unconfirm")) {
                criteria.andKeyNameEqualTo("cskaoyan_mall_order_unconfirm");
            }
            if (entry.getKey().endsWith("order_unpaid")) {
                criteria.andKeyNameEqualTo("cskaoyan_mall_order_unpaid");
            }
            if (entry.getKey().endsWith("order_comment")) {
                criteria.andKeyNameEqualTo("cskaoyan_mall_order_comment");
            }
            system.setKeyValue(entry.getValue());
            system.setUpdateTime(new Date());
            systemMapper.updateByExampleSelective(system, systemExample);
        }
    }

    @Override
    public Map<String, String> queryWxConfiguration() {
        List<System> systemList = ConfigUtils.invoke(systemMapper,"cskaoyan_mall_wx_");
        Map<String, String> map = new HashMap<>(16);
        for (System system : systemList) {
            if ("cskaoyan_mall_wx_share".equals(system.getKeyName())) {
                map.put("cskaoyanmall_wx_share", system.getKeyValue());
            }
            if ("cskaoyan_mall_wx_index_brand".equals(system.getKeyName())) {
                map.put("cskaoyanmall_wx_index_brand", system.getKeyValue());
            }
            if ("cskaoyan_mall_wx_index_topic".equals(system.getKeyName())) {
                map.put("cskaoyanmall_wx_index_topic", system.getKeyValue());
            }
            if ("cskaoyan_mall_wx_index_hot".equals(system.getKeyName())) {
                map.put("cskaoyanmall_wx_index_hot", system.getKeyValue());
            }
            if ("cskaoyan_mall_wx_index_new".equals(system.getKeyName())) {
                map.put("cskaoyanmall_wx_index_new", system.getKeyValue());
            }
            if ("cskaoyan_mall_wx_catlog_goods".equals(system.getKeyName())) {
                map.put("cskaoyanmall_wx_catlog_goods", system.getKeyValue());
            }
            if ("cskaoyan_mall_wx_catlog_list".equals(system.getKeyName())) {
                map.put("cskaoyanmall_wx_catlog_list", system.getKeyValue());
            }
        }
        return map;
    }

    @Override
    public void updateWxConfiguration(Map<String, String> map) {
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        System system = new System();
        for (Map.Entry<String, String> entry : entrySet) {
            SystemExample systemExample = new SystemExample();
            SystemExample.Criteria criteria = systemExample.createCriteria();
            if (entry.getKey().endsWith("wx_share")) {
                criteria.andKeyNameEqualTo("cskaoyanmall_wx_share");
            }
            if (entry.getKey().endsWith("wx_index_brand")) {
                criteria.andKeyNameEqualTo("cskaoyan_mall_wx_index_brand");
            }
            if (entry.getKey().endsWith("wx_index_topic")) {
                criteria.andKeyNameEqualTo("cskaoyanmall_wx_index_topic");
            }
            if (entry.getKey().endsWith("wx_index_hot")) {
                criteria.andKeyNameEqualTo("cskaoyanmall_wx_index_hot");
            }
            if (entry.getKey().endsWith("wx_index_new")) {
                criteria.andKeyNameEqualTo("cskaoyanmall_wx_index_new");
            }
            if (entry.getKey().endsWith("wx_catlog_goods")) {
                criteria.andKeyNameEqualTo("cskaoyanmall_wx_catlog_goods");
            }
            if (entry.getKey().endsWith("wx_catlog_list")) {
                criteria.andKeyNameEqualTo("cskaoyanmall_wx_catlog_list");
            }
            system.setKeyValue(entry.getValue());
            system.setUpdateTime(new Date());
            systemMapper.updateByExampleSelective(system, systemExample);
        }
    }

}
